using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using XinBang.ShoppingCart;
using XinBang.BLL;
using XinBang.Entity;
using XinBang.Util;
using XinBang.Session;
using System.Collections.Generic;

namespace XinBang.Web
{
    public partial class ShoppingCartPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                shoppingCartList.DataSource = RecordUtil.ConvertShoppingCartToRecord();
                shoppingCartList.DataBind();
            }
        }

        protected void toPay_Click(object sender, EventArgs e)
        {
            try
            {
                Cart cart = Cart.GetShoppingCart();
                OrderBL ob = new OrderBL();
                RecordBL rb = new RecordBL();
                SendBL sb = new SendBL();
                Order order = new Order();
                WebApplication.GetCurrentSession().BeginTransaction();
                order.Memid = SessionUtil.GetMemberSession();
                order.Senid = sb.GetDefaultSendMethod();
                ob.InsertOrder(order);
                Order lastOrder = ob.GetLastOrderByMember(SessionUtil.GetMemberSession());
                IList<Record> list = RecordUtil.ConvertShoppingCartRecordList();
                decimal amountPrice = 0;
                foreach (Record record in list)
                {
                    record.Ordid = lastOrder;
                    amountPrice += record.GoodsPrice * record.Amount;
                    rb.InsertRecord(record);
                }
                lastOrder.OrderValue = amountPrice;
                ob.UpdateOrder(lastOrder);
                Cart.GetShoppingCart().ClearShoppingCart();
                WebApplication.GetCurrentSession().Transaction.Commit();
                Response.Redirect("~/ShoppingOrderPage.aspx");
            }
            catch (MissSessionException)
            {
                Response.Redirect("~/Admin/Index.aspx");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
